using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;

namespace WOWEquipOptimizer
{
    public struct EquipCom
    {
        private float[] m_EquipValues;
        private ReaderWriterLock m_RWL;
        private float[] m_BuffValues;
        private float[] m_BaseValues;
        private int m_Length;
        private int m_Mode;

        public int Mode
        {
            get { return m_Mode; }
            set { m_Mode = value; }
        }
        private int[] m_Index;
        private List<string> m_SpecialValues;

        public List<string> SpecialValues
        {
            get { return m_SpecialValues; }
            set { m_SpecialValues = value; }
        }
        public EquipCom(Character basechar)
        {
            string[] names = basechar.getAttributes();
            m_Length = //names.Length;
            m_Length = Enum.GetNames(typeof(AttributeName)).Length;
            m_EquipValues = new float[m_Length];
            m_BuffValues = new float[m_Length];
            m_BaseValues = new float[m_Length];
            int start = basechar.getAttributesStart();
            m_SpecialValues = new List<string>();
            m_Index = new int[Enum.GetNames(typeof(AttributeName)).Length];
            m_Mode = 0;
            for (int i = 0; i < m_Index.Length; i++)
            {
                m_Index[i] = i;
            }
            m_RWL = new ReaderWriterLock();
        }
        public float this[AttributeName key]
        {
            get
            {
                int index = m_Index[(int)key];
                if (index < m_Length && index >= 0)
                {
                    m_RWL.AcquireReaderLock(-1);
                    try
                    {
                        switch (m_Mode)
                        {
                            case 0:
                                return m_BaseValues[index];
                            case 1:
                                return m_BuffValues[index];
                            case 2:
                                return m_EquipValues[index];
                            default:
                                return (m_BaseValues[index] + m_BuffValues[index] + m_EquipValues[index]);
                        }
                    }
                    finally
                    {
                        m_RWL.ReleaseReaderLock();
                    }

                }
                else return 0;
            }
            set
            {
                int index = m_Index[(int)key];
                if (index < m_Length && index >= 0)
                {
                    m_RWL.AcquireWriterLock(-1);
                    try
                    {
                        switch (m_Mode)
                        {
                            case 0:
                                m_BaseValues[index] = value;
                                break;
                            case 1:
                                m_BuffValues[index] = value;
                                break;
                            case 2:
                                m_EquipValues[index] = value;
                                break;
                            default:
                                break;
                        }
                    }
                    finally
                    {
                        m_RWL.ReleaseWriterLock();
                    }
                }
            }
        }
        public void ResetBase()
        {
            m_BaseValues = new float[m_Length];
        }
        public void ResetBuffs()
        {
            m_BuffValues = new float[m_Length];
        }
        public void ResetEquip()
        {
            m_EquipValues = new float[m_Length];
            for (int i = 0; i < m_EquipValues.Length; i++)
            {
                m_EquipValues[i] = 0.0f;
            }
        }
    }
}
